#!/usr/bin/bash

bar=""
arry=("\\" "|" "/" "-")
file=$0
clipid=0

progress() {
    if [ $clipid -gt 0 ]; then
        disown $clipid
        kill -9 $clipid >/dev/null 2>&1
    fi

    let index=$1%4
    if  [ $1 -le 25 ]; then
        let color=41
        let bg=31
    elif [ $1 -le 50 ]; then
        let color=43
        let bg=33
    elif [ $1 -le 75 ]; then
        let color=44
        let bg=34
    else
        let color=42
        let bg=32
    fi
    bar=$(perl -e "print '#' x $1;")
    printf "\033[2;0H \033[${color};${bg}m%-s\033[0m %d %c\r" "$bar" "$1" "${arry[$index]}"

    runing $1 $color $bg $bar & 2>/dev/null
    clipid=$!
}

showMsg() {
    msg=$1
    clear
    printf "\033[0;0H"
    printf "\033[3B"
    echo -e $msg >> ./tmsg
    cat ./tmsg
}

runing() {
    let num=$1
    color=$2
    bg=$3
    bar=$4
    while [[ true ]]; do
        let num+=1
        let s=$num%4
        usleep 100000
        printf "\033[2;0H \033[${color};${bg}m%-s\033[0m %d %c\r" "$bar" "$1" "${arry[$s]}"
    done
}


clear
showMsg "\033[44;30m Install Docker-compose \033[0m"
cp ./lib/docker-compose /usr/bin/ >> ./tmsg
showMsg "done"
progress 10

showMsg "\033[44;30m Import Images \033[0m"
docker load -i ./images/jhh-db.tar.gz >> ./tmsg
showMsg "done"
progress 20
docker load -i ./images/jhh-php.tar.gz >> ./tmsg
showMsg "done"
progress 50
docker load -i ./images/jhh-hdfs.tar.gz >> ./tmsg
showMsg "done"
progress 100

showMSg
showMsg "\033[42;37m Completed \033[0m"
showMsg "\033[33m You can use 'docker-compose up -d' start hadoop server. \033[0m"


disown $clipid
kill -9 $clipid
rm -f ./tmsg
exit 0